#include <MAUtil/Moblet.h>
#include <MAUI/Screen.h>
#include <MAUI/Label.h>
#include <MAUI/Layout.h>
#include <MAUI/EditBox.h>
#include <MAUI/Font.h>

#include "MAHeaders.h"

#include "userInfo.h"

using namespace MAUtil;
using namespace MAUI;

UserScreen::UserScreen(Screen *previous, User *myUser) : previous(previous), myUser(myUser)
{
	createUI();
}

UserScreen::~UserScreen()
{
	delete mMainLayoutWidget;
}

void UserScreen::createUI()
{
	MAUI::Font* font = new MAUI::Font(RES_FONT);

	MAUI::WidgetSkin* buttonSkin = new MAUI::WidgetSkin(
	RES_SELECTED,   // Image for selected state.
	RES_UNSELECTED, // Image for unselected state.
	16, // X coordinate for start of center patch.
	32, // X coordinate for end of center patch.
	16, // Y coordinate for start of center patch.
	32, // Y coordinate for end of center patch.
	true,  // Is selected image transparent?
	true); // Is unselected image transparent?

	// Get the width of the display. We use this
	// to size our widgets.
	int displayWidth = EXTENT_X(maGetScrSize());

	mMainLayoutWidget = new MAUI::Layout(
		0,  // Left coordinate.
		0,  // Top coordinate.
		0,  // Width.
		0,  // Height.
		NULL, // Parent widget.
		1,  // Number of columns in the layout.
		5); // Number of rows in the layout.

	this->setMain(mMainLayoutWidget);

	mInstructions = new MAUI::Label(
		0, // Left.
		0, // Top.
		displayWidth, // Width.
		60, // Height.
		mMainLayoutWidget,   // Parent widget.
		"Enter your initials:", // Text label.
		0x000000, // Background color.
		font);    // Font.

	mInstructions->setMultiLine(false);

	mPasswordBox = new MAUI::EditBox(
		0, // Left.
		0, // Top (will be set by the layout).
		displayWidth, // Width.
		30, // Height.
		mMainLayoutWidget, // Parent widget.
		"AAA",    // Initial text content.
		0x555555, // Background color.
		font,  // Font.
		true,  // Respond to keypresses when selected.
		false, // Not multiline.
		3);   // Max number of characters in the edit box.

	mPasswordBox->activate();

	mPasswordBox->setSelected(true);

	mClearButton = new MAUI::Label(
		0, // Left.
		0, // Top (will be set by the layout).
		displayWidth, // Width.
		30, // Height.
		mMainLayoutWidget, // Parent widget.
		"Clear", // Button text.
		0,     // Background color (we will use the skin).
		font); // Font.

	// Set the skin of the widget to make it look like a button.
	mClearButton->setSkin(buttonSkin);

	// Centre the button text horizontally and vertically.
	mClearButton->setHorizontalAlignment(MAUI::Label::HA_CENTER);
	mClearButton->setVerticalAlignment(MAUI::Label::VA_CENTER);

	// The fourth widget is the Submit button.
	mSubmitButton = new MAUI::Label(
		0, // Left.
		0, // Top (will be set by the layout).
		displayWidth, // Width.
		30, // Height.
		mMainLayoutWidget, // Parent widget.
		"ENTER", // Button text.
		0,     // Background color (we will use the skin).
		font); // Font.

	// Set the skin of the widget to make it look like a button.
	mSubmitButton->setSkin(buttonSkin);

	// Centre the button text horizontally and vertically.
	mSubmitButton->setHorizontalAlignment(MAUI::Label::HA_CENTER);
	mSubmitButton->setVerticalAlignment(MAUI::Label::VA_CENTER);

	// Now the user interface widgets are created.
	}

void UserScreen::pointerPressEvent(MAPoint2d point)
{
	// Deselect the edit box.
	mPasswordBox->setSelected(false);

	// If the Clear button is clicked...
	if (mClearButton->contains(point.x, point.y))
	{
		// ...set the button's state to "selected". This
		// automatically changes the button's skin.
		mClearButton->setSelected(true);
	}
	else
	if (mSubmitButton->contains(point.x, point.y))
	{
		// Do the same for the Submit button.
		mSubmitButton->setSelected(true);
	}
	else
	{
		// Reselect the edit box.
		mPasswordBox->setSelected(true);
	}
}

void UserScreen::pointerReleaseEvent(MAPoint2d point)
{
	// Deselect the buttons so that the "unselected" skins
	// are redisplayed, and also reselect the edit box.
	mClearButton->setSelected(false);
	mSubmitButton->setSelected(false);
	mPasswordBox->setSelected(true);

	// If the pointer is inside the Clear button we call the
	// action method for the button.
	if (mClearButton->contains(point.x, point.y))
	{
		clearButtonAction();
	}

	// If the pointer is inside the Submit button we call the
	// action method for the button.
	if (mSubmitButton->contains(point.x, point.y))
	{
		submitButtonAction();
		previous->show();
	}
	// If the edit box was touched we display a textbox.
	// This primarily intended for devices that do not
	// have a key pad for text input.
	if (mPasswordBox->contains(point.x, point.y))
	{
		editBoxAction();
	}
}

void UserScreen::keyPressEvent(int keyCode, int nativeCode)
{
	switch (keyCode)
	{
		// Close the application if the back key, key 0,
		// or the left "softkey" is pressed (the left
		// softkey is usually located at the top left of
		// the keypad of phones that have a keypad, and
		// is often used as a back navigation key).
		case MAK_BACK:
			previous->show();
			break;
		case MAK_0:
		case MAK_SOFTLEFT:
			// Exit the application.
			maExit(0);
			break;
		case MAK_UP:
			// When the up key on the keypad is pressed,
			// we select the previous widget.
			selectPreviousWidget();
			break;
		case MAK_DOWN:
			// When the down key on the keypad is pressed,
			// we select the next widget.
			selectNextWidget();
			break;
		case MAK_FIRE:
			// When the "fire" (centre) key on the keypad
			// is pressed, we run the action associated
			// with the selected widget.
			runWidgetAction();
			break;
	}
}

void UserScreen::selectPreviousWidget()
{
	if (mPasswordBox->isSelected())
	{
		mPasswordBox->setSelected(false);
		mSubmitButton->setSelected(true);
	}
	else
	if (mClearButton->isSelected())
	{
		mClearButton->setSelected(false);
		mPasswordBox->setSelected(true);
	}
	else
	if (mSubmitButton->isSelected())
	{
		mSubmitButton->setSelected(false);
		mClearButton->setSelected(true);
	}
}

void UserScreen::selectNextWidget()
{
	if (mPasswordBox->isSelected())
	{
		mPasswordBox->setSelected(false);
		mClearButton->setSelected(true);
	}
	else
	if (mClearButton->isSelected())
	{
		mClearButton->setSelected(false);
		mSubmitButton->setSelected(true);
	}
	else
	if (mSubmitButton->isSelected())
	{
		mSubmitButton->setSelected(false);
		mPasswordBox->setSelected(true);
	}
}

void UserScreen::runWidgetAction()
{
	if (mPasswordBox->isSelected())
	{
		editBoxAction();
	}
	else
	if (mClearButton->isSelected())
	{
		clearButtonAction();
	}
	else
	if (mSubmitButton->isSelected())
	{
		submitButtonAction();
	}

	// Deselect the buttons so that the "unselected" skins
	// are redisplayed, and make the edit box selected.
	mClearButton->setSelected(false);
	mSubmitButton->setSelected(false);
	mPasswordBox->setSelected(true);
}

void UserScreen::clearButtonAction()
{
	// Empty the contents of the edit box.
	mPasswordBox->clearText();
}

void UserScreen::submitButtonAction()
{
	MAUtil::String passord = mPasswordBox->getText();	// get the users initials
	myUser->setName(mPasswordBox->getText());
}

void UserScreen::editBoxAction()
{
	maTextBox(
		L"Enter your Initials:", // Title as a wide string.
		L"", // Initial text as a wide string.
		mTextBoxBuffer, // Input text buffer.
		256, // Buffer length.
		MA_TB_TYPE_ANY);
}

void UserScreen::textBoxClosed(int result, int textLength)
{
	// If the user press Ok in the textbox...
   	if (MA_TB_RES_OK == result)
   	{
		char buffer[256];
		int i;
		for (i = 0; i < textLength; ++i)
		{
			// Convert to byte size char.
			buffer[i] = (char) mTextBoxBuffer[i];
		}
		// Null terminate string.
		buffer[i] = 0;

		// Now we set the contents of the password box.
		mPasswordBox->setText(buffer);

		// Uncomment this line to display the text in
		// the instructions label.
		//mInstructions->setCaption(buffer);
  	}
}

